<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type="primary" @click="adminVisible">添加管理员</el-button>
      <el-table :data="getAdminData" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" width="width"></el-table-column>
        <el-table-column prop="_id" label="ID" width="width"></el-table-column>
        <el-table-column label="用户名" width="width" prop="username"></el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="editAdmin(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeCategory(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="添加管理员" :visible.sync="addAdminVisible" width="50%">
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addAdminVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAdmin">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑管理员" :visible.sync="editAdminVisible" width="50%">
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAdminVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CategoryList',
  data() {
    return {
      addAdminVisible: false,
      addForm: {},
      getAdminData: [],
      // 编辑对话框的显示与隐藏
      editAdminVisible: false,
      // 编辑分类绑定数据
      editForm: {
        _id: 0
      }
    }
  },
  methods: {
    adminVisible() {
      this.addAdminVisible = true
    },
    async addAdmin() {
      this.addAdminVisible = true
      await this.$http.post(`rest/admin_users`, this.addForm)
      this.$message.success(`添加用户:${this.addForm.username}`)
      this.addAdminVisible = false
      this.getAdminList()
    },
    async getAdminList() {
      const { data: res } = await this.$http.get(`rest/admin_users`)
      this.getAdminData = res
    },
    async editAdmin(row) {
      this.editAdminVisible = true
      const res = await this.$http.get(`rest/admin_users/${row._id}`)
      this.editForm = res.data
    },

    async editSave() {
      await this.$http.put(
        `rest/admin_users/${this.editForm._id}`,
        this.editForm
      )
      this.editAdminVisible = false
      this.$message.success(`成功更新用户名：${this.editForm.username}`)
      this.getAdminList()
    },
    async removeCategory(row) {
      const confirm = await this.$confirm(
        `是否删除用户名：${row.username}?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirm === 'cancel') {
        return this.$message.info('取消删除操作')
      } else {
        const res = await this.$http.delete(`rest/admin_users/${row._id}`)
        this.$message.success(`成功删除用户名：${res.data.username}`)
        this.getAdminList()
      }
    }
  },
  created() {
    this.getAdminList()
  }
}
</script>

<style scoped>
.el-card {
  margin-top: 15px;
}
</style>